/* jzc 2020-02-03 */
#include <stdio.h>
#include <stdlib.h> // exit()
#include <fcntl.h>  // O_WRONLY
#include <sys/stat.h>
#include <time.h>   // time


int main(int argc, char **argv) {
    int fd;
    int n, i;
    char buffer[1024];
    time_t tp;

    printf("I am %d process.\n", getpid());  //说明进程ID

    if ((fd = open("fifo1", O_WRONLY)) < 0) { //以写打开一个fifo
        perror("Open FIFO Faied");
        exit(1);
    }

    for (i=0; i<10; i++) {
        time(&tp); //获取系统时间
        n = sprintf(buffer, "Process %d's time is %s\n", getpid(), ctime(&tp));
        printf("Send message: %s", buffer); // 打印写入到fifo中的信息
        if (write(fd, buffer, n+1) < 0) {
            perror("Write FIFO Failed!\n");
            close(fd);
            exit(1);
        }
        sleep(1);
    }
    close(fd);
    return 0;
}

